import mcApi from 'mc-c2b2m-operator-api';
import { deliveryMethodList } from '@/enums/productionEnum';
import { BasicColumn, FormSchema } from '@/components/Table';
import { SpecValueModel } from 'mc-c2b2m-operator-api/types/order';

const { SysModule } = mcApi;
const { UploadApi } = SysModule;

export const tableColumns: BasicColumn[] = [
  { title: '发货单号', dataIndex: 'id' },
  { title: '收货人', dataIndex: 'consigneeName' },
  { title: '收货电话', dataIndex: 'consigneePhone' },
  { title: '状态', dataIndex: 'status', slots: { customRender: 'status' } },
  { title: '物流/快递名称', dataIndex: 'logisticsCompanyName' },
  { title: '货运单号', dataIndex: 'logisticsTrackingNumber' },
  {
    title: '销售单号',
    dataIndex: 'orderCode',
    customRender: ({ record }) => {
      return record?.billItemList?.map?.((item: any) => item.orderCode)?.join('、') || '';
    },
  },
  { title: '发货时间', dataIndex: 'deliveryTime' },
];

export const tableFormSchema: FormSchema[] = [
  {
    field: 'orderCode',
    label: '订单号',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'deliverTime',
    label: '发货时间',
    component: 'RangePicker',
    colProps: { span: 6 },
  },
];

export const collapsePanelList = [
  { label: '基础信息', value: 'basic' },
  { label: '产品信息', value: 'product' },
  { label: '送货信息', value: 'deliver' },
];

export const basicFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: '发货单号',
    component: 'Input',
    colProps: { span: 12 },
    componentProps: {
      disabled: true,
      style: {
        width: '50%',
      },
    },
  },
  {
    required: true,
    field: 'billImageList',
    label: '出货图片',
    component: 'Upload',
    colProps: { span: 12 },
    componentProps: {
      api: UploadApi.uploadFile,
      maxNumber: 9,
    },
  },
];

export const productColumns: BasicColumn[] = [
  { title: '订单号', dataIndex: 'orderCode' },
  { title: '产品名称', dataIndex: 'spuName' },
  {
    title: '材质',
    dataIndex: 'materialSpecValueList',
    customRender: ({ text }) => handleSpecValueList(text),
  },
  {
    title: '印刷',
    dataIndex: 'printingSpecValueList',
    customRender: ({ text }) => handleSpecValueList(text),
  },
  {
    title: '工艺',
    dataIndex: 'processSpecValueList',
    customRender: ({ text }) => handleSpecValueList(text),
  },
];
function handleSpecValueList(list: SpecValueModel[]) {
  let text: string = '';
  list?.forEach?.((item) => {
    const { spuSpecName: name, spuSpecValueName: val } = item;
    text += `${text ? '+' : ''}${name}(${val})`;
  });
  return text;
}

export const deliverFormSchema: FormSchema[] = [
  {
    required: true,
    field: 'deliveryMethod',
    label: '运输方式',
    component: 'Select',
    colProps: { span: 8 },
    componentProps: {
      options: deliveryMethodList,
    },
  },
  {
    required: true,
    field: 'deliverTime',
    label: '发货日期',
    component: 'DatePicker',
    colProps: { span: 8 },
    componentProps: {
      style: { width: '100%' },
    },
  },
  {
    field: 'settlementAmount',
    label: '运费',
    component: 'InputNumber',
    colProps: { span: 8 },
    componentProps: {
      min: 0,
    },
  },
  {
    field: 'addr',
    label: '地区',
    slot: 'addr',
    colProps: { span: 8 },
  },
  {
    field: 'addrDetails',
    label: '详细地址',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'consigneeName',
    label: '收货人名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'consigneePhone',
    label: '收货人电话',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'logisticsCompanyName',
    label: '物流/快递名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'logisticsTrackingNumber',
    label: '货运单号',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'remark',
    label: '备注',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const productFormSchema: FormSchema[] = [
  {
    field: 'orderPlacerUserPhone',
    label: '下单手机号',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'orderCode',
    label: '订单号',
    component: 'Input',
    colProps: { span: 8 },
  },
];
